package com.ay.demo.guava.eventbus;

import java.util.concurrent.Executors;

import com.ay.demo.guava.eventbus.event.BaseEvent;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;

public class EventBusPublisher {

	private static class SingletonHolder {
		private static EventBus eventBus = new EventBus();
		private static EventBus asyncEventBus = new AsyncEventBus("async", Executors.newCachedThreadPool());
		private static EventBusPublisher instance = new EventBusPublisher();
	}
	
	private EventBusPublisher() {
		EventBusSubscriber subscriber = new EventBusSubscriber();
		SingletonHolder.eventBus.register(subscriber);
		SingletonHolder.asyncEventBus.register(new EventBusAsyncSubscriber());
	}
	
	public static EventBusPublisher getInstance() {
		return SingletonHolder.instance;
	}
	
	public void postEvent(BaseEvent event) {
		SingletonHolder.eventBus.post(event);
	}
	
	public void postAsynEvent(BaseEvent event) {
		SingletonHolder.asyncEventBus.post(event);
	}
	
}
